@extends('common.base-master')

@section('html-head')
    <link rel="stylesheet" href="{{url('css/style.css')}}">
    <link rel="stylesheet" href="{{url('css/common.css')}}">
    <link rel="stylesheet" href="{{url('css/public.css')}}">
    <style>
        body{
            background-color: #f3f3f3;
        }
        .title{text-align: center;margin-top: 2rem;color: #5f6363;font-size: 1.0rem;font-family: '黑体';}
        .weibo{margin-top: 3rem;text-align: center}
        .weibo span{display: block;color: #c6cac9}
        .weibo img{margin-top: 1rem}
        .hidden{display: none}
    </style>
@endsection

@section('html-body')
        <div>
            <div class="title">欢迎加入乌托邦联合计划</div>
            <div class="weui-cells">
                <div class="weui-cell weui-cell_vcode right_15--before">
                    <div class="weui-cell__hd">
                        <div class="weui-label input-label">手机号：</div>
                    </div>
                    <div class="weui-cell__bd">
                        <input type="tel" name="newphone" class="weui-input" id="phone" placeholder="请输入新手机号码">
                    </div>
                    <div class="weui-cell__ft">
                        <button type="button" class="weui-vcode-btn" id="get-code" style="color:#1cac1b; ">获取验证码</button>
                    </div>
                </div>
                <div class="weui-cell right_15--before">
                    <div class="weui-cell__hd">
                        <label class="weui-label input-label">验证码：</label>
                    </div>
                    <div class="weui-cell__bd">
                        <input name="code" class="weui-input" type="text" id="code" placeholder="请输入手机短信验证码">
                    </div>
                </div>
            </div>
            <!-- 按钮 -->
            <div class="demos-content-padded margin-t_20">
                <a href="javascript:void(0);" class="weui-btn bg-color__theme font-color_white bg-color__theme--active" id="btn_login" style="border: none;width: 96%;background-color: #1cac1b;">开通会员/登录</a>
            </div>

            <div class="weui-agree">
                <span class="weui-agree__text">
                    点击“开通会员”意味着您已经阅读、知晓并同意
                    <a href="{{route('Home.Page.page',['tag'=>'member_agreement'])}}" style="color: #1cac1b">《会员服务协议》</a>
                </span>
            </div>
            {{--<div class="weibo hidden">--}}
                {{--<span>------通过以下方式登录------</span>--}}
                {{--<img src="{{url('images/weibo.png')}}" />--}}
            {{--</div>--}}
        </div>
    <script>
        $(function () {
            function is_weixn(){
                var ua = navigator.userAgent.toLowerCase();
                if(ua.match(/MicroMessenger/i)=="micromessenger") {
                    return true;
                } else {
                    return false;
                }
            }

            if(!is_weixn()){
                $('.weibo').removeClass('hidden');
            }

            var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(19[0-9]{1}))+\d{8})$/;

            //获取短信验证码
            var countdown=60;
            var canGetCode = true;
            $('#get-code').click(function () {
                var obj = $(this);
                if(canGetCode){
                    var phone = $('#phone').val();
                    if(!myreg.test(phone)){
                        $.toast("请正确输入手机号码!", "forbidden");
                        return false;
                    }

                    $.ajax({
                        url:"{{route('Api.Auth.getVerifyCodeByPhone')}}",
                        type:"post",
                        dataType:"json",
                        data:{phone:phone},
                        success:function (object) {
                            if(object.level == 'success'){
                                settime(obj);
                            }else {
                                $.toast(object.message, "forbidden");
                            }
                        },
                        error:function (object) {
                            console.log(object);
                        }
                    });
                }
            });
            function settime(obj) { //发送验证码倒计时
                if (countdown == 0) {
                    canGetCode=true;
                    obj.removeClass('getting');
                    obj.html("获取验证码");
                    countdown = 60;
                    return;
                } else {
                    canGetCode=false;
                    obj.html("重新发送(" + countdown + ")");
                    obj.addClass('getting');
                    countdown--;
                }
                setTimeout(function(){settime(obj) },1000)
            }

            $('#btn_login').click(function () {
                var phone = $('#phone').val();
                var code  = $('#code').val();

                $.ajax({
                    url:"{{route('Api.Auth.signIn')}}",
                    type:'post',
                    dataType:'json',
                    data:{phone:phone,code:code},
                    success:function (obj) {
                        if(obj.level == 'success'){
                            if(obj.order_num != ""){
                                if(obj.order_num == "has_join"){
                                    $.toast(obj.message,function () {
                                        window.location.href="{{route('Home.Page.index')}}";
                                    });
                                }else{
                                    if(is_weixn()){
                                        startJsApiPay(obj.order_num);
                                    }else {
                                        startH5Pay(obj.order_num);
                                    }
                                }

                            }else {
                                $.toast(obj.message,function () {
                                    window.location.href="{{route('Home.Page.joinMember')}}";
                                });
                            }

                        }else {
                            $.toast(obj.message, "forbidden");
                        }
                    },
                    error:function (obj) {
                        
                    }
                });
            });

            function is_weixn(){
                var ua = navigator.userAgent.toLowerCase();
                if(ua.match(/MicroMessenger/i)=="micromessenger") {
                    return true;
                } else {
                    return false;
                }
            }

            function startJsApiPay(order_num) {
                $.ajax({
                    type:"post",
                    url:'{{route('Api.Payment.payByWechatJsApi')}}',
                    dataType:"json",
                    data:{
                        order_num:order_num
                    },
                    success:function(obj){
                        if(obj.level == "success"){
                            $.hideLoading();
                            WXPayment(obj.data);
                        }else {
                            $.toast(obj.message, "forbidden");
                        }
                    },
                    error:function(obj){
                        console.log(obj);
                    }
                });
            }

            function startH5Pay(order_num) {
                $.ajax({
                    type:"post",
                    url:'{{route('Api.Payment.payByWechatH5')}}',
                    dataType:"json",
                    data:{
                        order_num:order_num
                    },
                    success:function(obj){
                        if(obj.level == "success"){
                            $.hideLoading();
                            window.location.href = obj.data;
                        }else {
                            $.toast(obj.message, "forbidden");
                        }
                    },
                    error:function(obj){
                        console.log(obj);
                    }
                });
            }

            function WXPayment(configJSON) {
                if (typeof WeixinJSBridge === 'undefined') {
                    $.toast('请在微信在打开页面', "forbidden");
                    return false;
                }
                WeixinJSBridge.invoke('getBrandWCPayRequest', configJSON, function (res) {
                    switch (res.err_msg) {
                        case 'get_brand_wcpay_request:cancel':
                            alert('用户取消支付！');
                            break;
                        case 'get_brand_wcpay_request:fail':
                            alert('支付失败！（' + res.err_desc + '）');
                            break;
                        case 'get_brand_wcpay_request:ok':
                            $.toast('支付成功',function () {
                                window.location.href='{{route('Home.Page.index')}}';
                            });
                            break;
                        default:
                            alert(JSON.stringify(res));
                            break;
                    }
                });
            }
        });
    </script>
@endsection